package com.example.dllo.keepdemo.train.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.person.PersonMessageActivity;
import com.example.dllo.keepdemo.tools.CalculateTimeDifference;
import com.example.dllo.keepdemo.tools.RoundImageView;
import com.example.dllo.keepdemo.tools.VolleySingleton;
import com.example.dllo.keepdemo.train.bean.TrainDynamicBean;

/**
 * Created by dllo on 16/1/16.
 */
public class TrainDynamicListViewAdapter extends BaseAdapter {
    private Context context;
    private TrainDynamicBean data;

    public TrainDynamicListViewAdapter(Context context,TrainDynamicBean data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.getData().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_activity_traindynamic, parent, false);
            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }
        myViewHolder.imgHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,PersonMessageActivity.class);
                intent.putExtra("id",data.getData().get(position).getAuthor().get_id());
                context.startActivity(intent);
            }
        });
        myViewHolder.tvName.setText(data.getData().get(position).getAuthor().getUsername());
        myViewHolder.tvContent.setText(data.getData().get(position).getContent());
        myViewHolder.tvTime.setText(CalculateTimeDifference.timeDifferenceUTC(data.getData().get(position).getCreated()));
        myViewHolder.tvOrder.setText("完成" + data.getData().get(position).getMeta().getName());
        myViewHolder.tvCount.setText("第" + data.getData().get(position).getMeta().getCount() + "次");
        myViewHolder.tvPraise.setText(data.getData().get(position).getLikes() + "");
        myViewHolder.tvComment.setText(data.getData().get(position).getComments() + "");

        if (data.getData().get(position).getAuthor().getAvatar().equals("") || !(data.getData().get(position).getAuthor().getAvatar().endsWith(".jpg"))) {
            myViewHolder.imgHeader.setImageResource(R.mipmap.person_45_45);
        } else {
            ImageLoader.ImageListener listener = ImageLoader.getImageListener(myViewHolder.imgHeader,R.mipmap.person_45_45,R.mipmap.person_45_45);
            VolleySingleton.getImageLoader().get(data.getData().get(position).getAuthor().getAvatar(),listener);
        }


        if (data.getData().get(position).getPhoto().equals("")) {
            myViewHolder.imageView.setVisibility(View.GONE);
        } else {
            myViewHolder.imageView.setVisibility(View.VISIBLE);
            myViewHolder.imageView.setDefaultImageResId(R.mipmap.person_45_45);
            myViewHolder.imageView.setErrorImageResId(R.mipmap.person_45_45);
            myViewHolder.imageView.setImageUrl(data.getData().get(position).getPhoto(),VolleySingleton.getImageLoader());
        }
        return convertView;
    }

    class MyViewHolder {
        private RoundImageView imgHeader;
        private NetworkImageView imageView;
        private TextView tvName,tvContent,tvTime,tvOrder,tvCount,tvPraise,tvComment;
        public MyViewHolder(View view) {
            imgHeader = (RoundImageView) view.findViewById(R.id.img_item_activity_traindynamic_headpicture);
            imageView = (NetworkImageView) view.findViewById(R.id.img_item_activity_traindynamic_picture);
            tvName = (TextView) view.findViewById(R.id.tv_item_activity_traindynamic_name);
            tvContent = (TextView) view.findViewById(R.id.tv_item_activity_traindynamic_content);
            tvTime = (TextView) view.findViewById(R.id.tv_item_activity_traindynamic_time);
            tvOrder = (TextView) view.findViewById(R.id.tv_item_activity_traindynamic_meta_name);
            tvCount = (TextView) view.findViewById(R.id.tv_item_activity_traindynamic_meta_count);
            tvPraise = (TextView) view.findViewById(R.id.tv_item_activity_traindynamic_praisenum);
            tvComment = (TextView) view.findViewById(R.id.tv_item_activity_traindynamic_replynum);
        }
    }
}
